﻿Public Class InventoryBatchExplorer
    Inherits ExplorerTreeList

    Private aPL As PLBase
    Public Property aLotTranHelper As LotTransactionHelper

    Public Sub New(ByVal aQueryName As String)
        MyBase.New(aQueryName)
    End Sub

    Public Sub New()
        MyBase.New()
    End Sub

    Public Overridable Sub InvBatchExplorer_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        aLotTranHelper = New LotTransactionHelper()
    End Sub

    Public Overrides Sub PopulateDetailSection()
        Me.Cursor = Cursors.WaitCursor
        '
        If aGridControl IsNot Nothing Then
            aGridControl.Dispose()
            aGridControl = Nothing
        End If

        Dim frmCaption As String = String.Empty
        Dim DescriptionLang As String = "_PRM"

        Dim aguid As Guid = DirectCast(NodeDataRow(Me.KeyFieldName), Guid)
        Dim aMQ = MetaQuery.GetMetaQueryByName(NodeDataRow("MetaQuerie").ToString)
        aPL = PLBase.CreateInstanceOf(NodeDataRow("PL").ToString(), aMQ.QueryName)

        If Not aguid.Equals(Guid.Empty) Then
            aPL.SQLWhere = String.Format("{0} = '{1}'", aMQ.GUIDField, aguid)
        End If

        If AppManager.ApplicationOption.DescriptionColumnIndex = 3 Then
            DescriptionLang = "_ALT"
        Else
            DescriptionLang = "_PRM"
        End If

        If NodeDataRow.Table.Columns.Contains("Description") Then
            frmCaption = NodeDataRow("Descrip" & DescriptionLang)
        Else
            frmCaption = "Descrip" & DescriptionLang
        End If


        aGridControl = New GridExtPL() With {.Dock = DockStyle.Fill, .Pl = aPL, .Name = "InventoryBatch", .FormCtrlCD = PanelName}
        '
        AddHandler aGridControl.ValidateMasterRow, AddressOf ValidateMasterRow
        AddHandler aGridControl.ValidateDetailRow, AddressOf ValidateDetailRow

        AddHandler aGridControl.InitNewMasterRow, AddressOf InitNewMasterRow
        AddHandler aGridControl.InitNewDetailRow, AddressOf InitNewDetailRow
        '
        AddHandler aGridControl.DeleteRow, AddressOf DeleteRow
        '
        AddHandler aGridControl.ProcessCurrentDetailRecord, AddressOf ProcessCurrentDetailRecord
        AddHandler aGridControl.DetailCellValueChanged, AddressOf DetailCellValueChanged

        AddHandler aGridControl.MasterRowClick, AddressOf MasterRowClick
        AddHandler aGridControl.DetailRowClick, AddressOf DetailRowClick

        AddHandler aGridControl.DetailCustomRowCellEditForEditing, AddressOf DetailCustomRowCellEditForEditing
        '
        InitGridActionOption(aGridControl)
        '
        'On trouve la form pour effacer les controle dedans et ensuite remettre le bon.
        'Comme ca on change le Detail selon la ligne dans L'explorer
        For Each aFrm As XtraForm In Me.ParentForm.MdiChildren
            If aFrm.Name = PanelName Then
                aFrm.Controls.Clear()
                aFrm.Controls.Add(aGridControl)
                aFrm.Text = frmCaption
                aFrm.Show()
                aFrm.Focus()
            End If
            aFrm.Refresh()
        Next

        Me.Cursor = Cursors.Default
    End Sub

End Class
